/**
 * Storage二次封装
 * @author Aforce
 */
import config from './../config'
export default {
  /**
   *
   * @param {String} key:字段名
   * @param {*} val:存储内容
   */
  setItem(key, val) {
    let storage = this.getStroage()
    storage[key] = val
    window.localStorage.setItem(config.namespace, JSON.stringify(storage))
  },
  /** 更新所有*/
  setAllitem(val) {
    window.localStorage.setItem(config.namespace, JSON.stringify(val))
  },
  /**
   *
   * @param {String} key
   * @returns
   */
  getItem(key) {
    return this.getStroage()[key]
  },
  getStroage() {
    return JSON.parse(window.localStorage.getItem(config.namespace) || '{}')
  },
  /**
   *
   * @param {String} key :清除的字段名
   */
  clearItem(key) {
    let storage = this.getStroage()
    delete storage[key]
    window.localStorage.setItem(config.namespace, JSON.stringify(storage))
  },
  clearAll() {
    window.localStorage.clear()
  },
}
